package com.xiaoqi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoqi.constant.MessageConstant;
import com.xiaoqi.pojo.CheckItem;
import com.xiaoqi.pojo.PageResult;
import com.xiaoqi.pojo.QueryPageBean;
import com.xiaoqi.pojo.Result;
import com.xiaoqi.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author PC
 * creationTime: 2022/10/12 10:12
 */
@RequestMapping("/checkitem")
@RestController
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * 查询所有检查项
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<CheckItem> checkItemList = checkItemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
    }

    /**
     * 分页查询检查项通过项目名或者项目编号
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @GetMapping("/findPageByCodeOrName")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findPageByCodeOrName(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "queryString", defaultValue = "") String queryString) {
        //把参数加入到对象
        QueryPageBean queryPageBean = new QueryPageBean(currentPage, pageSize, queryString);

        //查询
        PageResult checkItemList = checkItemService.findPageByCodeOrName(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
    }

    /**
     * 新增检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/addCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result addCheckItem(@RequestBody CheckItem checkItem) {
        checkItemService.addCheckItem(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 通过id删除检查项
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delCheckItem(@RequestParam("id") Integer id) {
        checkItemService.delCheckItem(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 通过id编辑检查项
     *
     * @param checkItem
     */
    @PutMapping("/updateCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result updateCheckItem(@RequestBody CheckItem checkItem) {
        checkItemService.updateCheckItem(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}
